package cn.changeforyou.demo.netty.common;

/**
 * tcp协议
 * <p>       |   开头	        |项目	        |信息类型	                                                           |正文长度	 |正文	 |时间戳	 |结尾            |</p>
 * <p>      |固定值 #@$ybkj$@#	|固定值takeout   |1:服务器推送json, 无需响应 ,2.心跳包,3:请求包,需要响应,4:响应包     |正文长度	|正文	|时间戳	|固定值 #@$com.ybkj$@# |</p>
 * <p>      |    10	        |7	            |1	                                                                   |2	      |?	    |8	        |1              / </p>
 * <p>
 * <p>
 * websocket 协议 json
 * * <p>  |项目名  projectName    |信息类型	 msgType  |正文 content	 |时间戳 timestamp|</p>
 *
 * @author zhyu
 * @date 2021/3/15 16:28
 */
public class Data {

	public enum MsgType {

		json(0),
		heart(1),
		req(2),
		response(3),
		;

		MsgType(int code) {
			this.code = code;
		}

		private int code;

		public int getCode() {
			return code;
		}

		public static MsgType valueOf(byte msgType) {
			MsgType[] msgTypes = MsgType.values();
			for (MsgType type : msgTypes) {
				if (type.code == msgType) {
					return type;
				}
			}
			throw new IllegalArgumentException("消息类型不支持");
		}
	}

	private Data() {
	}

	public static Data instanceHeartBeat() {
		return new Data(MsgType.heart, HEART_BEAT_CONTENT);
	}

	public static Data instanceReq(Req req) {
		return new Data(MsgType.req, req);
	}

	public static Data instanceResponse(Res res) {
		return new Data(MsgType.response, res);
	}

	public static final String HEAD = "#@$ybkj$@#";
	public static final String PROJECT_NAME = "takeout";
	public static final String FOOT = "#@$com.ybkj$@#";
	public static final String HEART_BEAT_CONTENT = "heart beat";

	private String head;

	private String projectName;

	private MsgType msgType;

	private short contentLength;

	private Object content;

	private long timestamp;

	private String foot;

	public void setContent(Object content) {
		this.content = content;
	}

	public Data(MsgType msgType, Object content) {
		this.msgType = msgType;
		this.content = content;
		timestamp = System.currentTimeMillis();
		projectName = PROJECT_NAME;
	}

	public Data(String head, String projectName, MsgType msgType, short contentLength, Object content, long timestamp, String foot) {
		this.head = head;
		this.projectName = projectName;
		this.msgType = msgType;
		this.contentLength = contentLength;
		this.content = content;
		this.timestamp = timestamp;
		this.foot = foot;
	}


	public String getProjectName() {
		return projectName;
	}

	public MsgType getMsgType() {
		return msgType;
	}

	public Object getContent() {
		return content;
	}

	public long getTimestamp() {
		return timestamp;
	}

	@Override
	public String toString() {
		return "Data{" +
			       "head='" + head + '\'' +
			       ", projectName='" + projectName + '\'' +
			       ", msgType=" + msgType +
			       ", contentLength=" + contentLength +
			       ", content=" + content +
			       ", time=" + timestamp +
			       ", foot='" + foot + '\'' +
			       '}';
	}
}
